package com.cskaoyan.controller;


import com.cskaoyan.bean.BO.GoodsBO;
import com.cskaoyan.bean.BO.GoodsDeleteBO;
import com.cskaoyan.bean.PO.MarketCommentExample;
import com.cskaoyan.bean.PO.MarketGoods;
import com.cskaoyan.bean.VO.GoodsCatAndBrandVo;
import com.cskaoyan.bean.VO.GoodsDetailVo;
import com.cskaoyan.bean.VO.MarketGoodsVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.utils.MyGetCurrentTime;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author linhanxin
 * @date 2022/8/13.
 */
@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {
    @Autowired
    GoodsService goodsService;

    @RequiresPermissions("admin:goods:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info,String goodsSn,Integer goodsId, String name){
        CommonData<MarketGoodsVo> data = goodsService.query(info,goodsSn,goodsId,name);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("admin:goods:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody GoodsDeleteBO goodsDeleteBO){
        goodsService.delete(goodsDeleteBO.getId());
        return BaseRespVo.ok("成功");
    }

    @RequiresPermissions("admin:goods:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GoodsBO goodsBO){
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        goodsBO.getGoods().setAddTime(currentTime);
        goodsBO.getGoods().setUpdateTime(currentTime);
        goodsService.createGoods(goodsBO);
       return BaseRespVo.ok(0,null,"成功");
    }

    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand(){
        GoodsCatAndBrandVo goodsCatAndBrandVo = goodsService.catAndBrand();
        return BaseRespVo.ok(0,goodsCatAndBrandVo,"成功");
    }

    @RequiresPermissions("admin:goods:read")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        GoodsDetailVo data = goodsService.detail(id);
        return BaseRespVo.ok(0,data,"成功");
    }

    @RequiresPermissions("admin:goods:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GoodsBO goodsBO){
        goodsService.updateGoods(goodsBO);
        return BaseRespVo.ok(0,null,"成功");
    }
}
